package com.luhui.codereview.listeners

import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileEditor.FileEditorManagerEvent
import com.intellij.openapi.fileEditor.FileEditorManagerListener
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.luhui.codereview.containsEditor
import com.luhui.codereview.putLiutener

/**
 * @author  luhui
 * @date  2019/10/24 23:45
 * @version 1.0
 */
class FileListener(val project: Project) : FileEditorManagerListener {

    override fun fileOpened(source: FileEditorManager, file: VirtualFile) {
        //文件被打开,标红有问题的代码
        addMouseListener()
    }


    override fun selectionChanged(event: FileEditorManagerEvent) {
        //切换文件，主要是实现这个方法，在切换文件时候，去看看这个文件有没有记录的行数
        addMouseListener()
    }


    private fun addMouseListener() {
        val fileEditorManager = FileEditorManager.getInstance(project) ?: return
        val editor = fileEditorManager.selectedTextEditor ?: return
        if (!containsEditor(editor)) {
            val fileEditorMouseListener = FileEditorMouseListener(editor,project)
            editor.addEditorMouseListener(fileEditorMouseListener)
            putLiutener(editor, fileEditorMouseListener)
        }
    }

}